package com.test31_IO应用;

import com.test31_IO应用.util.InputUtil;

import java.util.Arrays;

class StudentMsg implements Comparable<StudentMsg> {
    private long id;
    private String name;
    private int count;

    public StudentMsg(long id, String name, int count) {
        this.id = id;
        this.name = name;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public int compareTo(StudentMsg o) {
        return o.count - this.count;
    }

    @Override
    public String toString() {
        return "【候选人】编号：" + this.id + "姓名：" + this.name + "、票数：" + this.count;
    }
}

interface IVoteService {
    /** 投票开始 */
    public void start();
    /** 显示投票结果 */
    public StudentMsg[] show();
    /** 获取投票结果 */
    public StudentMsg result();
}

class VoteServiceImpl implements IVoteService {
    private StudentMsg[] students = new StudentMsg[] {
            new StudentMsg(1, "张三", 0),
            new StudentMsg(2, "李四", 0),
            new StudentMsg(3, "王五", 0),
            new StudentMsg(4, "赵六", 0)
    };

    @Override
    public void start() {
        boolean flag = true;
        while (flag) {
            int choose = InputUtil.getInt("请输入班长候选人代号（数组0结束）：", "此选票无效，请输入正确的候选人代号！");
            switch (choose) {
                case 0:
                    flag = false;   // 结束选票
                    break;
                case 1:
                    this.students[0].setCount(this.students[0].getCount() + 1);
                    break;
                case 2:
                    this.students[1].setCount(this.students[1].getCount() + 1);
                    break;
                case 3:
                    this.students[2].setCount(this.students[2].getCount() + 1);
                    break;
                case 4:
                    this.students[3].setCount(this.students[3].getCount() + 1);
                    break;
                default:
                    System.err.println("此选票无效，请输入正确的候选人代号！");
            }
        }
    }

    @Override
    public StudentMsg[] show() {
        return this.students;
    }

    @Override
    public StudentMsg result() {
        Arrays.sort(this.students); // 进行结果排序
        return this.students[0];
    }
}

class VoteFactory {
    private VoteFactory() {
    }

    public static IVoteService getInstance() {
        return new VoteServiceImpl();
    }
}

class VoteMain {
    private IVoteService voteService;

    public VoteMain() {
        this.voteService = VoteFactory.getInstance();
        this.printStudentInfo(this.voteService.show());
        System.out.println("================ 开始进行班长选举 ================");
        this.voteService.start();
        System.out.println("================ 班长选举投票结束 ================");
        this.printStudentInfo(this.voteService.show());
        System.out.println("================ 得到班长投票结果 ================");
        StudentMsg result = this.voteService.result();
        System.out.println("投票最终结果：" + result.getName() + "同学，最后以" + result.getCount() + "票当选班长！");
    }

    private void printStudentInfo(StudentMsg[] students) {
        Arrays.stream(students).forEach(System.out::println);
    }
}

/**
 * 投票选举班长
 */
public class YootkDemo_05 {

    public static void main(String[] args) {
        new VoteMain();
    }
}
